package com.bamboo.framework.framework.exception;

import com.bamboo.framework.common.format.ReturnMsg;
import com.qiniu.common.QiniuException;
import io.lettuce.core.RedisConnectionException;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author: 郭晴
 * @Date: 2018/12/25 15:23
 * @Description: 自定义异常处理
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class CustomExceptionHandler {


  @ExceptionHandler(RedisConnectionException.class)
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  public ReturnMsg handlerRedisConnectionException(RedisConnectionException ex) {
    return new ReturnMsg(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
  }

  @ExceptionHandler(QiniuException.class)
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  public ReturnMsg handlerQiniuException(QiniuException ex) {
    log.error(ex.response.toString());
    System.out.println(ex.response.toString());
    System.out.println(ex.error());
    return new ReturnMsg(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
  }

  @ExceptionHandler(IOException.class)
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  public ReturnMsg handlerIOException(IOException ex) {
    log.error(ex.getMessage());
    return new ReturnMsg(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
  }

  /**
   * 库异常
   */
  @ExceptionHandler(Exception.class)
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  public ReturnMsg handlerMyException(Exception ex) {
    log.error(ex.getMessage());
    return new ReturnMsg(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
  }
}
